
(function() {
	var app = angular.module('assetManagementApp', [ 'ui.bootstrap' ]);
	app
			.controller(
					'assetManagementController',
					function($http,$scope, $log, $uibModal) {
						var basePath = $scope.getAppBasePath();
						$scope.insertComponent = {};
						$scope.deleteComponent = {};
						var controller = this;
						controller.parameter = {};
						controller.queryParameter = "";
						this.warningConfigList = [];
						this.indexList = [];
//						$scope.assertMangeUrl = "http://10.4.31.20:3309";
						$scope.assertexportUrl =  basePath + "/data/exportHostList";
// 						$scope.assertexportUrl =  "http://10.7.83.70:48081/exportHostList1";
						$scope.listid=[];
						this.queryCondition = {
							"regionIndex" : "",
							"roomnameIndex" : "",
							"racknameIndex" : "",
							"ipAddrIndex":"",
							"page" : 1,
							"pageSize" : 5,
						};
						this.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0
						};

						$scope.clickEvent = function() {
							controller.queryCondition.page = controller.queryCondition.forwordPage;
							loadData();
						}

						// 跳转页数，回车事件
						$scope.enterEvent = function(e) {
							var keycode = window.event ? e.keyCode : e.which;
							if (keycode == 13) {
								$scope.clickEvent();
							}
						}

						this.pageChanged = function() {
							loadData();
						}
						// 完成全选全部选事件
						$("#check_all").click(
								function() {
									$(".check_item").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item",
										function() {
											if ($(".check_item:checked").length == $(".check_item").length) {
												$("#check_all").prop("checked",
														true);
											}
											if ($(".check_item:checked").length == 0) {
												$("#check_all").prop("checked",
														false);
											}
										});
						
						
						// 新增配置
						this.insertConfig = function() {
							$scope.insertdata = {};
							var modalInstanceAdd = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/insertAssetManagement.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'insertAssetManagementController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});

							modalInstanceAdd.result.then(function(result) {
								loadData();
							});
						}
						
						 var arr3=[
					            'ID','所属域','系统名称','机房','机架','设备存放位置','所属应用系统','主机用途','P/N', '序列号','型号描述','配置项名称','IP地址','掩码','网关','管理卡地址','配置项分类','搜索代码','配置项标识','备注','设备名称','资产编号','使用状态','维护厂家','维护联系人', '维护有效期','维护联系电话','区域', '购置日期','电口网卡数量','出保日期','是否与其他系统共用','CPU情况','CPU型号','CPU主频','CPU类型', '数据库版本', '品牌类型','磁盘信息', '硬盘大小','分区配置','分区大小','可用的接口卡数量','实物编码', '风扇数量', 'FC对端','是否配光驱','是否有分区','是否配磁带机','HBA卡数量','主机服务器标识', '型号','安装的磁盘数量','I/O通道数量','IP地址','逻辑CPU数量','逻辑分区数量','生产厂商','满配CPU个数','满配磁盘个数','满配磁盘插槽数量','内存型号','内存数量','管理卡交换机端口','网卡数量','网络对端','光口网卡数量', '操作系统','外系统接口','口令','物理CPU数量', '物理内存大小','电源数量', '项目名称','维护人','机房位置','SAN','服务器类型','交换机端口','软件应用','软件配置','TPCC值','已用内存插槽数量','使用人'
					        ];
						// 编辑配置
						this.editConfig = function() {
							var region= controller.queryCondition.regionIndex ||"";
							var roomName = controller.queryCondition.roomnameIndex ||"";
							var rackName = controller.queryCondition.racknameIndex ||"";
							var ipAddr = controller.queryCondition.ipAddrIndex ||"";
							var page = controller.queryCondition.page;
							var pageSize = controller.queryCondition.pageSize;
			
							var conditons =  new Array();
							var serialon;
							var ids = "";
							$.each($(".check_item:checked"), function() {
								var id = "";  
		                        var sh_region;
								id = $(this).parents("tr").find("td:eq(1)").text();
								
								conditons.push(id);
								ids += $(this).parents("tr").find("td:eq(1)").text()+",";
							})
							ids = ids.substring(0,ids.length-1);
							
							if(conditons == ""){
								alert("请勾选需要编辑的配置");
								return;
							}else if(conditons.length > 1){
								alert("只能选中一个进行配置");
								return;
							}else{
								$scope.listid=conditons[0];
								$scope.editdata = {};
								var modalInstanceAdd = $uibModal
										.open({
											backdrop : 'static',
											keyboard : false,
											scope : $scope,
											// animation :
											// $scope.animationsEnabled,// 打开时的动画开关
											templateUrl : 'js/controller/configuration.management/editAssetManagement.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
											controller : 'editAssetManagementController',// 这是模态框的控制器,是用来控制模态框的
											ariaLabelledBy : 'modal-title',
											ariaDescribedBy : 'modal-body',
										// size : "lg",// 模态框的大小尺寸
										});
								modalInstanceAdd.opened.then(function(){//模态窗口打开之后执行的函数  
//								
									$.ajax({
										 type : "POST",
										 url : basePath + "/data/queryHostInfo",
										 dataType : "json",
										 data:{
											 "region":region,
											 "roomName":roomName,
											 "rackName":rackName,
											 "ipAddr":ipAddr,
											 "page":page,
											 "pageSize":pageSize
										 },
				                        success:function (res) {
				                        	 var jsonResult =  angular.fromJson(res);
				                        	 
				                        	var arr1=[];var arr2=[];var arr=[];
				                            for(var i=0; i<jsonResult.data.length;i++) {
				                            	
				                                if(conditons[0] == jsonResult.data[i].id){
				                                	
				                                    	for(var j in jsonResult.data[i]){
//				                                    		
				                                    		   arr1.push(j);
						                                    	arr2.push(jsonResult.data[i][j]);
				                                    	}
				                                    	 for (var o=0;o<arr1.length;o++){
				                                    		 	var obj={};
				                                    		 	obj.assetname=arr1[o];
						                                    	obj.assetvalue=arr2[o];
						                                    	obj.chainname=arr3[o];
						                                    	arr.push(obj);
				                                    	 }
				                                    	  	
						                                    $scope.listIndex= arr;
//						                                  
				                                  
				                                }
				                                else {
				                                    console.log("没有匹配！");
				                                }
				                             
				                            }
				                            
				                            $scope.$apply();
				                        },erro:function(){
				                        	 console.log("请求错误！")
				                        }
				                    });
								console.log($scope.listid);
//									
					          	});  
								modalInstanceAdd.result.then(function(result) {
									alert("编辑成功！")
									loadData();
								});
			
							}
							
						}
						//删除配置
						this.delConfig = function() {
							var conditons =  new Array();
							var ids = "";
							var id = "";
							$.each($(".check_item:checked"), function() {
								id = $(this).parents("tr").find("td:eq(1)").text();
								conditons.push(id);		
								ids = ids + id + ";";
							})
							console.log(conditons);
							// 如果没有选中的话
							if (conditons == "") {
								alert("请勾选需要删除的配置");
								return;
							} else {
							
								if (confirm("确认删除该的配置吗?")) {
									// 保存后台调用
									$.ajax({
												type : "POST",
												url : basePath + "/data/deleteHostInfo",
												dataType : "json",
//												traditional: true,
												data : {"ids" : ids},
//												contentType : 'application/json;charset=utf-8',//指定为json类型 
												success : function(data) {
													console.log("00000");
													alert("删除成功");
													loadData();
												}
											});

								}
							}
						}
						//导入文件
						
						this.leadingIn = function() {
							var modalInstanceAdd = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/flieAssetManagement.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'fileAssetManagementController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});

							modalInstanceAdd.result.then(function(result) {
//								alert("导入成功");
								loadData();
							});
						}
						
						//机房域下拉框
						   $scope.shroomname = [
						                        {id : "1", name : "北泰5F"},
								        	    {id : "2", name : "建安2-3"},
								        	    {id : "3", name : "建安2-5"},
								        	    {id : "4", name : "南汇3-2机房"},
								        	    {id : "5", name : "南汇3-4机房"},
								        	    {id : "6", name : "南汇3-5机房"},
								        	    {id : "7", name : "云莲5F北"},
								        	    {id : "8", name : "云莲5F南"},
								        	    {id : "9", name : "云莲6F包房"},
								        	    {id : "10", name : "云莲6F北"},
								        	    {id : "11", name : "云莲6F南"},
								        	    {id : "12", name : "全部"},
								        	    ];
						 //机架域下拉框
						  
						 //所属域下拉框
						   $scope.shregion = [
						                        {id : "1", name : "EDA(EDA)"},
								        	    {id : "2", name : "BSS(BSS)"},
								        	    {id : "3", name : "ITOM(ITM)"},
								        	    {id : "4", name : "MSS(MSS)"},
								        	    {id : "5", name : "OSS(OSS)"},
								        	    {id : "6", name : "应用系统(APS)"},
								        	    ];
						 
						// 搜索事件
						this.search = function() {
							loadData();
						}

						// 重置事件
						this.reset = function() {
							this.queryCondition = {
									"regionIndex" : "",
									"roomnameIndex" : "",
									"racknameIndex" : "",
									"ipAddrIndex":"",
									"page" : 1,
									"pageSize" : 5,
								};
						}
						
						// 程序初期化
						this.init = function() {
							loadData();
							
						}
						this.init();

						// 加载数据
						function loadData() {
							if ($(".check_item:checked").length == 0) {
								$("#check_all").prop("checked", false);
							}
							
							// 参数获取
							var region= controller.queryCondition.regionIndex ||"";
							var roomName = controller.queryCondition.roomnameIndex ||"";
							var rackName = controller.queryCondition.racknameIndex ||"";
							var ipAddr = controller.queryCondition.ipAddrIndex ||"";
							var page = controller.queryCondition.page;
							var pageSize = controller.queryCondition.pageSize;
			
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/queryHostInfo",
							 dataType : "json",
							 //contentType : "application/json",
//							 data : JSON.stringify(controller.parameter),
							 data:{
								 "region":region,
								 "roomName":roomName,
								 "rackName":rackName,
								 "ipAddr":ipAddr,
								 "page":page,
								 "pageSize":pageSize
							 },
							 success : function(res) {
								 var jsonResult =  angular.fromJson(res)
								
							 controller.assetManagementList = jsonResult.data;
							 controller.paginationInfo.totalPages = jsonResult.totalPages;
							 controller.paginationInfo.totalElements =jsonResult.totalElements;
							
							
							 //手动刷新
							 	$scope.$apply();  
							 },errro:function(){
								 console.log("请求错误！")
							 }
							 });

						}

					})
			.controller(
					'insertAssetManagementController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						this.indexList = [];
						this.kpiList = [];
						
						// 程序初期化
						this.init = function() {
						}
						this.init();
						
						
						
						$scope.save = function() {

							// INSERT值获取
						
							// 增加所传的参数
							// console.log($("form").serialize()); // FirstName=Bill&LastName=Gates
							// 后台调用
							if($("input[name=serialon]").val()==""){
								console.log($("input[name=serialon]").val())
								alert("请输入序列号！");
							}else{
								$.ajax({
									type : "POST",
									url : basePath + "/data/insertHostInfo",
									dataType : "json",
									data :$("form").serialize(),
									success : function(data) {
										alert("插入成功！");
										$uibModalInstance.close();
										
									}
								});
								
							}
							
							
						};
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};
					})
					//编辑配置
					.controller(
							'editAssetManagementController',
							function($scope, $uibModalInstance, $http, $log) {
								var basePath = $scope.getAppBasePath();
								var controller = this;
								this.indexList = [];
								this.kpiList = [];
								// 程序初期化
								this.init = function() {
								}
								this.init();
								$scope.id=$scope.$parsent
								$scope.edit = function() {
									
	                                // INSERT值获取
//										console.log($("form").serialize());
									// 后台调用
	                                if($("#editrow div").find("input").val()!= $scope.listid){
	                                	console.log($scope.listid);
	                                	alert("ID不能编辑！")
	                                }else{
	                                	$.ajax({
											type : "POST",
											url : basePath + "/data/updateHostInfo",
											dataType : "json",
											data :$("form").serialize(),
											success : function(data) {
												console.log($("#editrow div").eq(2).find("input").val());
												alert("编辑成功");
												   $uibModalInstance.close();
											},erro:function(err){
												console.log("请求失败");
											}
										});
										
	                                }
									
								};
								$scope.cancel = function() {
									// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
									$uibModalInstance.dismiss('cancel');
								};
							})
				//导入文件
				.controller(
					'fileAssetManagementController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						this.indexList = [];
						this.kpiList = [];
						
						// 程序初期化
						this.init = function() {
						}
						this.init();
						$scope.filesave= function() {
				            var formData = new FormData($('#uploadForm')[0]);
				            $.ajax({
				                type: 'post',
				                url : basePath + "/data/importHostList",
				                data: formData,
				                cache: false,
				                processData: false,
				                contentType: false,
				                success:function(data){
				                	alert(data.desc);
				                	$uibModalInstance.close();
				                	console.log(data);
				                },error:function(data){
				                	alert("请求错误！")
				                }
				            });
				        };

//					
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};
					})
			
})();
